package my.jwds.p819;



import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MostCommonWord {
    public static void main(String[] args) {
        Solution obj = new Solution();
        System.out.println(obj.mostCommonWord("Bob", new String[]{"hit"}));
    }

    static class Solution {
        public String mostCommonWord(String paragraph, String[] banned) {
            Set<String> exclude = new HashSet<>();
            for (String s : banned) {
                exclude.add(s);
            }
            if (isLetter(paragraph.charAt(paragraph.length()-1)))paragraph+=" ";
            int start = 0;
            Map<String,Integer> countMap = new HashMap<>();
            int max = 0;
            String res = null;
            for (int i = 0; i < paragraph.length(); i++) {

                if (!isLetter(paragraph.charAt(i))){
                    String s = paragraph.substring(start,i).toLowerCase();
                    start = i+1;
                    if (s.isEmpty() || exclude.contains(s)) continue;
                    Integer count = countMap.get(s);
                    if (count == null){
                        count = 0;
                    }
                    count ++;
                    if (count > max){
                        max = count;
                        res = s;
                    }
                    countMap.put(s,count);
                }
            }
            return res;
        }
        boolean isLetter(char c){
            return c >= 'a' && c <= 'z' || c>='A' && c<='Z';
        }
    }
}